@@ -0,0 +1,47 @@ |
||
| 1 |
+module Agents |
|
| 2 |
+ class PostAgent < Agent |
|
| 3 |
+ cannot_be_scheduled! |
|
| 4 |
+ |
|
| 5 |
+ description <<-MD |
|
| 6 |
+ Post Agent receives events from other agents and send those events as the contents of a post request to a specified url. `post_url` field must specify where you would like to receive post requests and do not forget to include URI scheme(`http` or `https`) |
|
| 7 |
+ MD |
|
| 8 |
+ |
|
| 9 |
+ event_description <<-MD |
|
| 10 |
+ Does not produce any event. |
|
| 11 |
+ MD |
|
| 12 |
+ |
|
| 13 |
+ def default_options |
|
| 14 |
+ {
|
|
| 15 |
+ :post_url => "http://www.example.com", |
|
| 16 |
+ :expected_receive_period_in_days => 1 |
|
| 17 |
+ } |
|
| 18 |
+ end |
|
| 19 |
+ |
|
| 20 |
+ def working? |
|
| 21 |
+ last_receive_at && last_receive_at > options[:expected_receive_period_in_days].to_i.days.ago |
|
| 22 |
+ end |
|
| 23 |
+ |
|
| 24 |
+ def validate_options |
|
| 25 |
+ unless options[:post_url].present? && options[:expected_receive_period_in_days].present? |
|
| 26 |
+ errors.add(:base, "post_url and expected_receive_period_in_days are required fields") |
|
| 27 |
+ end |
|
| 28 |
+ end |
|
| 29 |
+ |
|
| 30 |
+ def post_event(uri,event) |
|
| 31 |
+ req = Net::HTTP::Post.new(uri.request_uri) |
|
| 32 |
+ req.form_data = event |
|
| 33 |
+ if uri.scheme == "https" |
|
| 34 |
+ Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) { |http| http.request(req) }
|
|
| 35 |
+ else |
|
| 36 |
+ Net::HTTP.start(uri.hostname, uri.port) { |http| http.request(req) }
|
|
| 37 |
+ end |
|
| 38 |
+ end |
|
| 39 |
+ |
|
| 40 |
+ def receive(incoming_events) |
|
| 41 |
+ incoming_events.each do |event| |
|
| 42 |
+ uri = URI options[:post_url] |
|
| 43 |
+ post_event uri, event.payload |
|
| 44 |
+ end |
|
| 45 |
+ end |
|
| 46 |
+ end |
|
| 47 |
+end |
@@ -0,0 +1,71 @@ |
||
| 1 |
+require 'spec_helper' |
|
| 2 |
+ |
|
| 3 |
+describe Agents::PostAgent do |
|
| 4 |
+ before do |
|
| 5 |
+ @valid_params = {
|
|
| 6 |
+ :name => "somename", |
|
| 7 |
+ :options => {
|
|
| 8 |
+ :post_url => "http://www.example.com", |
|
| 9 |
+ :expected_receive_period_in_days => 1 |
|
| 10 |
+ } |
|
| 11 |
+ } |
|
| 12 |
+ |
|
| 13 |
+ @checker = Agents::PostAgent.new(@valid_params) |
|
| 14 |
+ @checker.user = users(:jane) |
|
| 15 |
+ @checker.save! |
|
| 16 |
+ |
|
| 17 |
+ @event = Event.new |
|
| 18 |
+ @event.agent = agents(:jane_weather_agent) |
|
| 19 |
+ @event.payload = {
|
|
| 20 |
+ :somekey => "somevalue", |
|
| 21 |
+ :someotherkey => {
|
|
| 22 |
+ :somekey => "value" |
|
| 23 |
+ } |
|
| 24 |
+ } |
|
| 25 |
+ |
|
| 26 |
+ @sent_messages = [] |
|
| 27 |
+ stub.any_instance_of(Agents::PostAgent).post_event { |uri,event| @sent_messages << event}
|
|
| 28 |
+ end |
|
| 29 |
+ |
|
| 30 |
+ describe "#receive" do |
|
| 31 |
+ it "checks if it can handle multiple events" do |
|
| 32 |
+ event1 = Event.new |
|
| 33 |
+ event1.agent = agents(:bob_weather_agent) |
|
| 34 |
+ event1.payload = {
|
|
| 35 |
+ :xyz => "value1", |
|
| 36 |
+ :message => "value2" |
|
| 37 |
+ } |
|
| 38 |
+ |
|
| 39 |
+ lambda {
|
|
| 40 |
+ @checker.receive([@event,event1]) |
|
| 41 |
+ }.should change { @sent_messages.length }.by(2)
|
|
| 42 |
+ end |
|
| 43 |
+ end |
|
| 44 |
+ |
|
| 45 |
+ describe "#working?" do |
|
| 46 |
+ it "checks if events have been received within expected receive period" do |
|
| 47 |
+ @checker.should_not be_working |
|
| 48 |
+ Agents::PostAgent.async_receive @checker.id, [@event.id] |
|
| 49 |
+ @checker.reload.should be_working |
|
| 50 |
+ two_days_from_now = 2.days.from_now |
|
| 51 |
+ stub(Time).now { two_days_from_now }
|
|
| 52 |
+ @checker.reload.should_not be_working |
|
| 53 |
+ end |
|
| 54 |
+ end |
|
| 55 |
+ |
|
| 56 |
+ describe "validation" do |
|
| 57 |
+ before do |
|
| 58 |
+ @checker.should be_valid |
|
| 59 |
+ end |
|
| 60 |
+ |
|
| 61 |
+ it "should validate presence of post_url" do |
|
| 62 |
+ @checker.options[:post_url] = "" |
|
| 63 |
+ @checker.should_not be_valid |
|
| 64 |
+ end |
|
| 65 |
+ |
|
| 66 |
+ it "should validate presence of expected_receive_period_in_days" do |
|
| 67 |
+ @checker.options[:expected_receive_period_in_days] = "" |
|
| 68 |
+ @checker.should_not be_valid |
|
| 69 |
+ end |
|
| 70 |
+ end |
|
| 71 |
+end |